package com.nf.xiaomi.dao.impl;

import com.nf.dbutils.SqlExecutor;
import com.nf.dbutils.handlers.BeanListHandler;
import com.nf.dbutils.handlers.ScalarHandler;
import com.nf.xiaomi.dao.OrderDao;
import com.nf.xiaomi.dao.OrderDetailDao;
import com.nf.xiaomi.entity.OrderEntity;
import com.nf.xiaomi.util.SqlExecutorUtils;

import java.util.List;

public class OrderDetailDaoImpl implements OrderDetailDao {
    //未付款--1
    //待发货（发货）--2
    //待收货--3
    //已完成--4
    SqlExecutor executor = SqlExecutorUtils.getExecutor();
    public List<OrderEntity> getAll() {
        String sql = "select od.product_detail_id,o.order_id,u.user_account,a.user_name,a.user_phone,a.address_content\n" +
                ",o.order_mode,od.product_detail_amount,od.product_detail_price,o.order_date,o.nstate from order_info o\n" +
                "left join order_detail od on o.order_id = od.order_id\n" +
                "left join user_info u on o.user_id = u.user_id\n" +
                "left join address a on u.address_id = a.address_id";
        return executor.query(sql, new BeanListHandler<>(OrderEntity.class));
    }

    public List<OrderEntity> getAll(Integer orderState) {
        String sql = "select od.product_detail_id,o.order_id,u.user_account,a.user_name,a.user_phone,a.address_content\n" +
                ",o.order_mode,od.product_detail_amount,od.product_detail_price,o.order_date,o.nstate from order_info o\n" +
                "left join order_detail od on o.order_id = od.order_id\n" +
                "left join user_info u on o.user_id = u.user_id\n" +
                "left join address a on u.address_id = a.address_id\n" +
                "where o.nstate = ?";
        return executor.query(sql, new BeanListHandler<>(OrderEntity.class),orderState);
    }

    public List<OrderEntity> getAll(Long account) {
        String sql = "select od.product_detail_id,o.order_id,u.user_account,a.user_name,a.user_phone,a.address_content\n" +
                ",o.order_mode,od.product_detail_amount,od.product_detail_price,o.order_date,o.nstate from order_info o\n" +
                "left join order_detail od on o.order_id = od.order_id\n" +
                "left join user_info u on o.user_id = u.user_id\n" +
                "left join address a on u.address_id = a.address_id\n" +
                "where u.user_account = ?";
        return executor.query(sql, new BeanListHandler<>(OrderEntity.class), account);
    }

    public List<OrderEntity> confirmOrder(Long account, Integer orderID) {
        String sql = "select od.product_detail_id,o.order_id,u.user_account,a.user_name,a.user_phone,a.address_content\n" +
                ",o.order_mode,od.product_detail_amount,od.product_detail_price,o.order_date,o.nstate from order_info o\n" +
                "left join order_detail od on o.order_id = od.order_id\n" +
                "left join user_info u on o.user_id = u.user_id\n" +
                "left join address a on u.address_id = a.address_id\n" +
                "where u.user_account = ? and o.order_id = ?";
        BeanListHandler<OrderEntity> listHandler = new BeanListHandler<>(OrderEntity.class);
        return executor.query(sql, listHandler, account, orderID);
    }

    public Integer insert(OrderEntity order) {
        String sql = "insert into order_detail(order_id,product_detail_id,product_detail_amount,product_detail_price,nstate)" +
                "values(?,?,?,?,?)";
        return executor.update(sql,order.getOrderID(),order.getProductDetailID(),order.getProductDetailAmount(),order.getProductDetailPrice(),order.getState());
    }

    public Integer deleteByID(Integer orderID) {
        String sql = "delete from order_detail where order_id =?";
        return executor.update(sql, orderID);
    }
}
